/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn;

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

/**
 * <p>Project: jse2303 - Exce2
 * <p>Powered by webrx On 2023-07-18 15:50:19
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Exce2 {
    public static void main(String[] args) {
        //猜数游戏
        Random rand = new Random();
        int n = rand.nextInt(1, 101);
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入[1-100]整数:");
            int t = 0;
            try {
                t = sc.nextInt();
                if (t < 1 || t > 100) {
                    throw new InputException();
                }
            } catch (InputMismatchException e) {
                System.out.println("请输入整数");
                String s = sc.next();
                if ("exit".equalsIgnoreCase(s) || "quit".equalsIgnoreCase(s) || "bye".equalsIgnoreCase(s)) {
                    break;
                } else {
                    continue;
                }
            } catch (InputException e) {
                System.out.println("输入有错误，请输入1-100之间的随机整数");
                continue;
            } catch (Exception e) {
                System.out.println("输入有未知错误");
                continue;
            }
            if (t > n) {
                System.out.println("\033[31m太大了\033[0m");
            } else if (t < n) {
                System.out.println("\033[31m太小了\033[0m");
            } else {
                System.out.println("\033[32m恭喜：你猜对了。\033[0m");
                break;
            }
        }
        System.out.println("游戏结束。");
    }
}
